package com.campus.web.controller.study;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.campus.app.service.EveningSubjectsService;
import com.campus.app.service.TeachersService;
import com.campus.commons.entity.Class;
import com.campus.commons.entity.ClassRoom;
import com.campus.commons.entity.EveningSubject;
import com.campus.commons.utils.Error;
import com.campus.commons.utils.Result;
import com.campus.web.controller.management.ClassRoomController;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 晚自习排课表 前端控制器
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@RestController
@RequestMapping("/even-subj")
public class EveningSubjectController {
    @Autowired
    private EveningSubjectsService eveningSubjectsService;

    @Autowired
    private TeachersService teachersService;

    @GetMapping("/")
    public ModelAndView toCheckEvening() {
        return new ModelAndView("learn/evening_subject/index");
    }

    @GetMapping("/edit")
    public ModelAndView toEdit(int id) {
        ModelAndView modelAndView = Edit();
        modelAndView.addObject("edit", true);
        modelAndView.addObject("info",eveningSubjectsService.getById(id));
        return modelAndView;
    }
    private ModelAndView Edit() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("class", Class.classes.values());
        modelAndView.addObject("classroom", ClassRoomController.getClassRooms());

        modelAndView.setViewName("learn/evening_subject/edit");
        return modelAndView;
    }
    @GetMapping("/add")
    public ModelAndView toAdd() {
        ModelAndView edit = Edit();
        edit.addObject("edit", false);
        return edit;
    }



    @PostMapping("/all")
    public String getAllSubject(int page,int limit) {
        return JSON.toJSONString(eveningSubjectsService.getAllSubjects(page, limit), SerializerFeature.WriteDateUseDateFormat);
    }
@ApiOperation("获取当天晚自习课表")
    @PostMapping("/nowSubject")
    public Result getNowSubjects() {
        return eveningSubjectsService.getNowSubjects();
    }
    //      按条件查询
    @PostMapping("/search")
    public String getSubjects(int page, int limit, Date start, Date end, String className) {
        return JSON.toJSONString(eveningSubjectsService.getSubjects(page, limit, start, end, className), SerializerFeature.WriteDateUseDateFormat);
    }

    //   添加
    @PostMapping("/add")
    public String addSubject(EveningSubject eveningSubject) {
        return JSON.toJSONString(eveningSubjectsService.addSubject(eveningSubject));
    }

    //    删除
    @PostMapping("/del")
    public String delSubject(int id) {
        return JSON.toJSONString(eveningSubjectsService.delSubject(id));
    }

    //    修改
    @PostMapping("/update")
    public String upSubject(EveningSubject eveningSubject) {
        return JSON.toJSONString(eveningSubjectsService.upSubject(eveningSubject));
    }
    @PostMapping("/upload")
    public Result uploadFile(MultipartFile file) {
        if (file!=null) {
            try {
                return eveningSubjectsService.uploadSubject(file.getInputStream(), file.getOriginalFilename());
            } catch (IOException e) {
                e.printStackTrace();
                return Result.error(Error.File_E1R, "读取异常");
            }
        }
        return null;
    }


//    对缓存的处理


}

